package core

import (
	"os"
	"text/template"
)

var GetControllerGenerator = new(ControllerGenerator)

type ControllerGenerator struct{}

func (c *ControllerGenerator) GenerateController(tableName string, StructName string, structName string, moduleName string) {

	param := Param{
		TableName:    tableName,
		StructName:   StructName,
		VariableName: structName,
		ModuleName:   moduleName,
	}
	tmplFile := "templates/controller.tmpl"
	outputFile := "code/controller/" + StructName + "Controller.go"
	//outputFile := filepath.Join(dir, "code", "controller", StructName+"Controller.go")

	//创建模板
	tmpl, err := template.ParseFiles(tmplFile)
	if err != nil {
		panic(err)
	}

	// 创建输出文件
	out, err := os.Create(outputFile)
	if err != nil {
		panic(err)
	}
	defer out.Close()

	// 执行模板，将结果写入输出文件
	err = tmpl.Execute(out, param)
	if err != nil {
		panic(err)
	}

}
